ggcharts画漂亮的图(折线图、棒棒糖图、哑铃图、金字塔图) – 欧阳松的博客 您所在的位置:网站首页 哑铃 图 ggcharts画漂亮的图(折线图、棒棒糖图、哑铃图、金字塔图) – 欧阳松的博客

ggcharts画漂亮的图(折线图、棒棒糖图、哑铃图、金字塔图) – 欧阳松的博客

2023-08-20 07:32| 来源: 网络整理| 查看: 265

如果你会ggplot2,其实折线图、棒棒糖图、哑铃图、金字塔图你都可以画,但是代码很长,ggcharts是一个简洁的包,一行代码画好,其实也是很香的啊

教程来自ggcharts 快速绘制优美图形 (qq.com)

这个包包的功能其实很多,我当一下搬运工演示一下

安装包很简单,如果没有安装,只需要一行命令安装

install.packages("ggcharts") ## 加载这个包包 library(ggcharts) ## 载入需要的程辑包:ggplot2 分面条形图

首先看ggolot2画这种图有多么的麻烦,代码那么长

library(dplyr) library(ggplot2) library(ggcharts) data("biomedicalrevenue") biomedicalrevenue%>% filter(year%in%c(2012,2015,2018))%>% group_by(year)%>% top_n(10,revenue)%>% ungroup()%>% mutate(company=tidytext::reorder_within(company,revenue,year))%>% ggplot(aes(company,revenue))+ geom_col()+ coord_flip()+ ##转置 tidytext::scale_x_reordered()+ ## 设置x排序,需要安装tidytext facet_wrap(vars(year),scales="free_y") ##分面,按y分面

如果用ggcharts,代码是那么短,而且蓝蓝的配色,很舒服

biomedicalrevenue %>% filter(year %in% c(2012, 2015, 2018)) %>% bar_chart(x = company, y = revenue, facet = year, top_n = 10)

折线图 data("revenue_wide") line_chart(data = revenue_wide, x = year, y = Roche:Bayer) + labs(x = "Year", y = "Revenue (Billion USD)")

## 这里要注意,只要github的包才能用line_chart函数,CRAN那个包不行 棒棒糖图 biomedicalrevenue %>% filter(year == 2018) %>% lollipop_chart(x = company, y = revenue, threshold = 30) + labs( x = NULL, y = "Revenue", title = "Biomedical Companies with Revenue > $30Bn." ) + scale_y_continuous( labels = function(x) paste0("$", x, "Bn."), expand = expansion(mult = c(0, .05)) )

哑铃图 data("popeurope") dumbbell_chart( data = popeurope, x = country, y1 = pop1952, y2 = pop2007, top_n = 10, point_colors = c("lightgray", "#494F5C") ) + labs( x = NULL, y = "Population", title = "Europe's Largest Countries by Population in 2007" ) + scale_y_continuous( limits = c(0, NA), labels = function(x) paste(x, "Mn.") )

双面柱状图 data(mtcars) mtcars_z


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有